Skip to content

Fix IOBuf TLS block pool self-loop caused by double-return (#3243)#3244

Open
walterzhaoJR wants to merge 3 commits intoapache:masterfrom
walterzhaoJR:fix-3243
Open

Fix IOBuf TLS block pool self-loop caused by double-return (#3243)#3244
walterzhaoJR wants to merge 3 commits intoapache:masterfrom
walterzhaoJR:fix-3243

Conversation

@walterzhaoJR
Copy link

release_tls_block() and release_tls_block_chain() do not guard against a block being returned to TLS when it is already the list head. The assignment b->portal_next = block_head becomes b->portal_next = b, forming a self-loop that causes remove_tls_block_chain() or share_tls_block() to spin forever, silently hanging the thread at exit.

Fix:

  • release_tls_block(): add early return when b == block_head, skipping the duplicate insertion.
  • release_tls_block_chain(): during the existing chain walk, check each node against block_head before linking. Return early if overlap is detected so that num_blocks stays consistent with the actual list length (remove_tls_block_chain verifies this via CHECK_EQ).

Add three unit tests that reproduce the self-loop through:

  1. Direct double release_tls_block() of the same block.
  2. release_tls_block_chain() with a chain overlapping the TLS head.
  3. IOBufAsZeroCopyOutputStream::BackUp() followed by a second release.

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

What is changed and the side effects?

Changed:

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

release_tls_block() and release_tls_block_chain() do not guard against
a block being returned to TLS when it is already the list head.  The
assignment `b->portal_next = block_head` becomes `b->portal_next = b`,
forming a self-loop that causes remove_tls_block_chain() or
share_tls_block() to spin forever, silently hanging the thread at exit.

Fix:
- release_tls_block(): add early return when b == block_head, skipping
  the duplicate insertion.
- release_tls_block_chain(): during the existing chain walk, check each
  node against block_head before linking.  Return early if overlap is
  detected so that num_blocks stays consistent with the actual list
  length (remove_tls_block_chain verifies this via CHECK_EQ).

Add three unit tests that reproduce the self-loop through:
1. Direct double release_tls_block() of the same block.
2. release_tls_block_chain() with a chain overlapping the TLS head.
3. IOBufAsZeroCopyOutputStream::BackUp() followed by a second release.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a TLS IOBuf block-pool corruption case where double-returning a block can create a cyclic free-list and hang the thread on later traversal (notably at thread exit cleanup).

Changes:

  • Add a guard in release_tls_block() intended to prevent re-inserting the TLS head and forming a self-loop.
  • Add an overlap check in release_tls_block_chain() intended to prevent linking a chain that would create a cycle and to keep num_blocks consistent.
  • Add three unit tests reproducing the reported hang scenarios from issue #3243.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.

File Description
src/butil/iobuf_inl.h Adds a duplicate-return guard in release_tls_block() to prevent self-looping the TLS free list.
src/butil/iobuf.cpp Adds an overlap check and uses a saved old_head when returning a block chain to TLS.
test/iobuf_unittest.cpp Adds regression tests that attempt to reproduce and detect the TLS free-list cycle described in #3243.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

fix review comment
add new ut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] IOBuf TLS block pool: double-return of a Block creates a self-loop in portal_next linked list, causing thread hang

2 participants